#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
import json
import time

def test_pdf_generator():
    """测试PDF生成器"""
    print("测试PDF生成器...")
    
    try:
        from emergency_plan.pdf_generator import EmergencyPlanPDFGenerator
        
        generator = EmergencyPlanPDFGenerator()
        print("PDF生成器创建成功")
        
        # 测试数据
        plan_data = [
            {
                "field_name": "安全事件监测",
                "content": "测试内容"
            }
        ]
        
        # 测试生成PDF
        pdf_bytes = generator.generate_pdf_bytes("测试事件", "测试子类", plan_data)
        print(f"PDF生成成功，大小: {len(pdf_bytes)} 字节")
        
        return True
    except Exception as e:
        print(f"PDF生成器测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_save_plan_simple():
    """测试简单的保存预案请求"""
    print("\n测试保存预案接口...")
    
    # 最简单的测试数据
    data = {
        "event_type": "test",
        "event_subtype": "test",
        "plan": [
            {
                "field_name": "安全事件监测",
                "content": "test"
            },
            {
                "field_name": "安全事件分析、定级、上报",
                "content": "test"
            },
            {
                "field_name": "应急响应处置",
                "content": "test"
            },
            {
                "field_name": "安全事件取证和溯源",
                "content": "test"
            },
            {
                "field_name": "安全事件总结、结束响应",
                "content": "test"
            }
        ]
    }
    
    try:
        response = requests.post(
            'http://localhost:8080/api/v1/save_plan',
            json=data,
            headers={'Content-Type': 'application/json'},
            timeout=30
        )
        
        print(f"状态码: {response.status_code}")
        print(f"响应头: {dict(response.headers)}")
        print(f"响应内容: {response.text}")
        
        if response.status_code == 200:
            result = response.json()
            print("✅ 保存预案成功！")
            return True
        else:
            print("❌ 保存预案失败")
            return False
            
    except Exception as e:
        print(f"请求异常: {e}")
        import traceback
        traceback.print_exc()
        return False

def main():
    print("调试测试开始...")
    
    # 等待服务器启动
    print("等待服务器启动...")
    time.sleep(5)
    
    # 测试PDF生成器
    pdf_ok = test_pdf_generator()
    
    if pdf_ok:
        # 测试保存预案接口
        save_ok = test_save_plan_simple()
        
        if save_ok:
            print("\n🎉 所有测试通过！")
        else:
            print("\n❌ 保存预案接口测试失败")
    else:
        print("\n❌ PDF生成器测试失败")

if __name__ == "__main__":
    main() 